package com.atguigu.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.hosp.repository.DepartmentRepository;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;
import com.atguigu.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentRepository departmentRepository;

    @Override
    public void saveDepartment(Map<String, Object> paramMap) {
        String string = JSONObject.toJSONString(paramMap);
        Department department = JSONObject.parseObject(string, Department.class);
        Department targetDepartment  = departmentRepository.getByHoscodeAndDepcode(department.getHoscode(), department.getDepcode());
        if (targetDepartment != null) {
            department.setId(targetDepartment.getId());
            department.setCreateTime(targetDepartment.getCreateTime());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }else {
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }
    }

    @Override
    public Page<Department> selectPage(int page, int limit, DepartmentQueryVo departmentQueryVo) {
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
        Pageable pageable = PageRequest.of((page-1), limit, sort);
        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo,department);
        ExampleMatcher exampleMatcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Example example = Example.of(department, exampleMatcher);
        Page pageModel = departmentRepository.findAll(example, pageable);
        return pageModel;
    }

    @Override
    public void remove(String hoscode, String depcode) {
        Department department = departmentRepository.getByHoscodeAndDepcode(hoscode, depcode);
        if (department != null) {
            departmentRepository.deleteById(department.getId());
        }
    }

    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {
        //封装返回的数据集合
        List<DepartmentVo> list = new ArrayList<>();
        //查询所有科室
        List<Department> departmentList = departmentRepository.findByHoscode(hoscode);
        //根据大科室进行分类
        Map<String, List<Department>> departmentMap = departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));
        for (Map.Entry<String, List<Department>> entry : departmentMap.entrySet()) {
            //封装大科室信息
            DepartmentVo departmentVo = new DepartmentVo();
            String bigCode = entry.getKey();
            String bigname = entry.getValue().get(0).getBigname();
            departmentVo.setDepcode(bigCode);
            departmentVo.setDepname(bigname);

            //封装小科室信息
            List<DepartmentVo> list1 = new ArrayList<>();
            for (Department department : entry.getValue()) {
                DepartmentVo departmentVo1 = new DepartmentVo();
                departmentVo1.setDepname(department.getDepname());
                departmentVo1.setDepcode(department.getDepcode());
                list1.add(departmentVo1);
            }
            departmentVo.setChildren(list1);
            list.add(departmentVo);
        }


        return list;
    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {
        return departmentRepository.getByHoscodeAndDepcode(hoscode, depcode);
    }

    @Override
    public String getDepname(String hoscode, String depcode) {

        return departmentRepository.getByHoscodeAndDepcode(hoscode,depcode).getDepname();
    }


}
